usbPipe0      EQU 0000h
usbPipe1      EQU 0100h
usbPipe2      EQU 0200h
usbPipe3      EQU 0300h

pipeBulk       EQU 20h
pipeInt        EQU 30h

usbPacket8    EQU 0100h
usbPacket16   EQU 0200h
usbPacket32   EQU 0400h
usbPacket64   EQU 0800h

pipeIn        EQU 1
pipeOut       EQU 2

pipeBitIn     EQU 0
pipeBitOut    EQU 1



cbNewData EQU 0
cbStall   EQU 1
cbNak     EQU 2

uGetVersion		EQU 0
uDriverInit		EQU 1
uIsDeviceConnected	EQU 2
uHostInit		EQU 3
uHostKill		EQU 4
uReadDescriptor		EQU 5
uGetIDs			EQU 6
uGetClass		EQU 7
uConfigureDevice	EQU 8
uSetupOutPipe		EQU 9
uSetupInPipe		EQU 10
uFindDescriptor		EQU 11
uSendData		EQU 12
uAutoSetup		EQU 13
uSendControlData	EQU 14
uReqData             EQU 15
uDriverKill          EQU 16
uMouseInit           EQU 17
uMouseGetKey         EQU 18 
uWaitTimerB		EQU 19
uPipeInfo		EQU 20
uSetupLog		EQU 21
uSendDataPart		EQU 22
uFindPipe		EQU 23
uStopLog		EQU 24
uLogCustom		EQU 25
uKBDVersion		EQU 26
uMSDVersion		EQU 27
uMSD_Initialize		EQU 28
uUFI_Initialize		EQU 29
uUFI_Read		EQU 30
uUFI_Write		EQU 31
uFAT_Initialize 	EQU 32
uFAT_lookupPath		EQU 33
uDOS_openFile		EQU 34
uDOS_fileSeek		EQU 35
uDOS_fileRead		EQU 36
uDOS_fileWrite		EQU 37
uDOS_createFile		EQU 38
uDOS_createDirectory	EQU 39
uDOS_deleteFileEntry	EQU 40
uDOS_countFilesInDir	EQU 41
uDOS_getNextFile	EQU 42
uFAT_getStartingCluster	EQU 43
uMSD_ImportVariable	EQU 44
uMSD_ExportVariable	EQU 45
uDOS_renameFileEntry	EQU 46
uFAT_nameConvertTo11	EQU 47
uFAT_nameConvertFrom11	EQU 48
uFAT_findFileInCluster	EQU 49
uDOS_getFileSize	EQU 50
uDOS_getDirEntry	EQU 51
uKBDInit             EQU 52
uKBDGetKey           EQU 53
uKBDGetMods          EQU 54
uKBDCheckDirect      EQU 55
uSilverInit          EQU 56
uSilverSend          EQU 57
uSetCallBack         EQU 58
uPadInit             EQU 59
uPadSetup            EQU 60
uPadStart            EQU 61
uGetErrorCode        EQU 62
uPadVersion          EQU 63
uInternalInfo        EQU 64
uSendControlDataOut  EQU 65
uVernierVersion      EQU 66
uVernierInit         EQU 67
uVernierSendCmd      EQU 68
uVernierQuickCmd     EQU 69
uVernierGetMeas      EQU 70
uVernierToDegree     EQU 71
uDOS_updateAttributes       EQU 72
uPumpOn              EQU 73
uPumpOff             EQU 74
uPump                EQU 75
uMSD_cacheOn         EQU 76
uMSD_cacheOff        EQU 77
umsdDetect           EQU 78

USBDriverEntryPoint EQU 404Ch




MSDBOTState					EQU 0								;1 - bulk-only transport state (0 - nothing, 1 - getting data, 2 - CSW)
MSDLBA						EQU MSDBOTState+1				;4 - LBA address for read/write routines
MSDPartStart				EQU MSDLBA+4					;4 - start of FAT16 partition
MSDBPS						EQU MSDPartStart+4			;2 - bytes per sector (typically 512)
MSDDataInAddr				EQU MSDBPS+2					;2 - address of incoming/outgoing data
MSDDataInExpected			EQU MSDDataInAddr+2			;2 - data expected to be received/sent
MSDSPC						EQU MSDDataInExpected+2		;1 - sectors per cluster (typically 2, 4, and so on)
MSDFATSectors				EQU MSDSPC+1					;2 - number of sectors per FAT
MSDRootDirSectors			EQU MSDFATSectors+2			;2 - number of root directory sectors (something like 32)
MSDFirstDataSector		EQU MSDRootDirSectors+2		;4 - first data sector
MSDFirstRootDirSecNum	EQU MSDFirstDataSector+4	;2 - first sector of root
MSDFATStart					EQU MSDFirstRootDirSecNum+2;2 - first sector of first FAT
MSDOutPipe					EQU MSDFATStart+2				;1 - bulk out pipe
MSDInPipe					EQU MSDOutPipe+1				;1 - bulk in pipe
MSDFAT_sector				EQU MSDInPipe+1				;2 - temporary storage for FAT_getNextCluster output
MSDDescBuf					EQU MSDFAT_sector+2			;128 - buffer for descriptors
MSDBulkBuf					EQU MSDDescBuf+128			;31 - incoming data buffer
MSDPacketData				EQU MSDBulkBuf+31				;64 - buffer to build data packets

mouseRight    EQU 1
mouseLeft     EQU 2
mouseUp       EQU 4
mouseDown     EQU 8

mouseBitRight EQU 0
mouseBitLeft  EQU 1
mouseBitUp    EQU 2
mouseBitDown  EQU 3

errPort82			EQU 1
errPort91Bit2			EQU 2 ;Stall
errPort91Bit4			EQU 3
errPort4C			EQU 4
errPort4D	    	       EQU 5
errPort3A	           	EQU 6
errPort81                   EQU 7
errPort8C                   EQU 8
errPort8F                   EQU 9
errEPIndex                  EQU 10
errEPGetDes                 EQU 11
errNoDevice                 EQU 12
errFindDescriptor           EQU 13
errBadClass                 EQU 14
errTimeout                  EQU 15
errBadID                    EQU 16
errReadDescriptor           EQU 17
errInit                     EQU 18
errAutoSetup                EQU 19
errDemo                     EQU 20
errFindPipe                 EQU 21
errStub                     EQU 22
errGetMaxPacketSize		EQU 23
errSetAddress               EQU 24
errMSDSendCmd               EQU 25	;Problem sending bulk-only in/out commands
errMSDLargeSect             EQU 26	;Bytes-per-sector is larger than 512 (not supported)
errMSDNoParts               EQU 27	;No FAT16 primary partitions were found
errMSDNoMoreMemory          EQU 28	;Function failed because no more memory available (this is a bad thing, delete incomplete file/directories)
errMSDInvalidPartition      EQU 29	;Partition is invalid (does not contain 0AA55h)
errMSDNotFile               EQU 30	;Path is a directory or volume label, not file
errMSDNotValidPath          EQU 31	;Path is not in any way valid :)
errMSDCorruptFile           EQU 32	;File is corrupted or otherwise not as expected
errMSDNotFound              EQU 33	;File/directory was not found
errMSDDuplicateFound        EQU 34 ;Duplicate calculator variable was found and was not overwritten
errMSDNotEnoughMem          EQU 35 ;Not enough memory to create new calculator variable
errMSDAlreadyExists         EQU 36	;*.8x* of same name already exists
errMSDStrNotFound           EQU 37 ;String variable from BASIC OpenLib/ExecLib function not found
errMSDVarEmpty              EQU 38 ;Variable was empty and transfer was not completed (this is bad, delete incomplete file)
errMSDNoParts32		EQU 39 ;FAT32 partitions were found, but none were FAT16 (just a friendly message)
errNoData                   EQU 40
errVersion                  EQU 41
errBadCmd                   EQU 42
errFeatureDisabled          EQU 43
errMSDFailedCmd             EQU 44
errMSDCmdTimeout            EQU 45
errMSDCmdRefused            EQU 46

descDevice    EQU 1
descConfig    EQU 2
descString    EQU 3
descInterface EQU 4
descEndpoint  EQU 5
descDQ        EQU 6
descOSC       EQU 7
descIP        EQU 8
descOnTheGo   EQU 9
descHID       EQU 21h
descReport    EQU 22h
descHub       EQU 29h

cacheFlagRAM EQU 0

kbdFlagNoLocks EQU 0
kbdFlagCapsLock EQU 1
kbdFlagNumLock EQU 2
kbdFlagScrollLock EQU 3

kbdLCtrlBit EQU 0
kbdLShiftBit EQU 1
kbdLAltBit EQU 2
kbdLGUIBit EQU 3
kbdRCtrlBit EQU 4
kbdRShiftBit EQU 5
kbdRAltBit EQU 6
kbdRGUIBit EQU 7

kbdLCtrl EQU 1
kbdLShift EQU 2
kbdLAlt EQU 4
kbdLGUI EQU 8
kbdRCtrl EQU 16
kbdRShift EQU 32
kbdRAlt EQU 64
kbdRGUI EQU 128

kbdK1         EQU 1Eh
kbdK2         EQU 1Fh
kbdK3         EQU 20h
kbdK4         EQU 21h
kbdK5         EQU 22h
kbdK6         EQU 23h
kbdK7         EQU 24h
kbdK8         EQU 25h
kbdK9         EQU 26h
kbdK0         EQU 27h

kbdKBackQuote EQU 35h       ;` ~
kbdKMinus     EQU 2Dh       ;- _
kbdKEqu       EQU 2Eh       ;= +
kbdKBackSpace EQU 2Ah
kbdKEsc       EQU 29h
kbdKTab       EQU 2Bh
kbdKCapsLock  EQU 39h
kbdKLBrack    EQU 2Fh
kbdKRBrack    EQU 30h
kbdKBackSlash EQU 31h
kbdKSemicolon EQU 33h
kbdKApostrophe EQU 34h
kbdKEnter     EQU 28h
kbdKComma     EQU 36h
kbdKPeriod    EQU 37h
kbdKSlash     EQU 38h
kbdKSpace     EQU 2Ch

kbdKA         EQU 4
kbdKB         EQU 5
kbdKC         EQU 6
kbdKD         EQU 7
kbdKE         EQU 8
kbdKF         EQU 9
kbdKG         EQU 0Ah
kbdKH         EQU 0Bh
kbdKI         EQU 0Ch
kbdKJ         EQU 0Dh
kbdKK         EQU 0Eh
kbdKL         EQU 0Fh
kbdKM         EQU 10h
kbdKN         EQU 11h
kbdKO         EQU 12h
kbdKP         EQU 13h
kbdKQ         EQU 14h
kbdKR         EQU 15h
kbdKS         EQU 16h
kbdKT         EQU 17h
kbdKU         EQU 18h
kbdKV         EQU 19h
kbdKW         EQU 1Ah
kbdKX         EQU 1Bh
kbdKY         EQU 1Ch
kbdKZ         EQU 1Dh

kbdKF1        EQU 3Ah
kbdKF2        EQU 3Bh
kbdKF3        EQU 3Ch
kbdKF4        EQU 3Dh
kbdKF5        EQU 3Eh
kbdKF6        EQU 3Fh
kbdKF7        EQU 40h
kbdKF8        EQU 41h
kbdKF9        EQU 42h
kbdKF10       EQU 43h
kbdKF11       EQU 44h
kbdKF12       EQU 45h

kbdKRight     EQU 4Fh
kbdKLeft      EQU 50h
kbdKDown      EQU 51h
kbdKUp        EQU 52h

kbdKSysRq     EQU 46h
kbdKScrollLock EQU 47h
kbdKBreak     EQU 48h
kbdKInsert    EQU 49h
kbdKHome      EQU 4Ah
kbdKPageUp    EQU 4Bh
kbdKDelete    EQU 4Ch
kbdKEnd       EQU 4Dh
kbdKPageDown  EQU 4Eh

kbdKNumLock   EQU 53h
kbdKNSlash    EQU 54h
kbdKNStar     EQU 55h
kbdKNMinus    EQU 56h
kbdKNPlus     EQU 57h
kbdKNEnter    EQU 58h
kbdKN1        EQU 59h
kbdKN2        EQU 5Ah
kbdKN3        EQU 5Bh
kbdKN4        EQU 5Ch
kbdKN5        EQU 5Dh
kbdKN6        EQU 5Eh
kbdKN7        EQU 5Fh
kbdKN8        EQU 60h
kbdKN9        EQU 61h
kbdKN0        EQU 62h
kbdKNPeriod   EQU 63h

U_CALL #macro label ; was at the beginning
 #ifndef USBDriverCall
  EXTERN USBDriverCall
 #endif
 #if"u&label"="uMouseInit"
       ld     ix,USBDriverCall+offCallBack
 #endif

 #if"u&label"="uPadInit"
       ld     ix,USBDriverCall+offCallBack
 #endif
 #if"u&label"="uSilverInit"
       ld     ix,USBDriverCall+offCallBack
 #endif
 #if"u&label"="uVernierInit"
       ld     ix,USBDriverCall+offCallBack
 #endif
 #if"u&label"="uKBDInit"
       ld     ix,USBDriverCall+offCallBack
 #endif
 #if"u&label"="uAutoSetup"
       ld     de,USBDriverCall+offCallBack
 #endif
	ld	a,u&label
	call	USBDriverCall
 .endm

U_CALL_INIT #macro label
 #ifndef USBDriverCall
  warning "U_CALL_INIT not in same namespace as USBDriverCall"
  EXTERN USBDriverCall
 #endif
       ld     hl,uuAppName                              
       rst    20h                                       
       B_CALL FindApp                                   
       jr     c,uuDone                                  
       ld     de,USBDriverCall                          
       ld     hl,uuDriverCode                           
       ld     bc,uuDriverSize                           
       ldir                                             
       ld     (USBDriverCall+uuMod1 + 1),a              
       in     a,(6)
       ld     (USBDriverCall+uuMod3 +1),a
       jr     uuDone
uuAppName:
       db     AppObj,"USBDRV8X",0
uuDriverCode:
       push   af                                        ;1
       in     a,(6)                                     ;3
       ld     (USBDriverCall+uuMod2 +1),a               ;6
       call   USBDriverCall+uuCall1                     ;9
       pop    af                                        ;10
       call   USBDriverEntryPoint                       ;13
       push   af                                        ;14
uuMod2 EQU $ - uuDriverCode
       ld     a,0                                       ;16
       out    (6),a                                     ;18
       pop    af                                        ;19
       ret                                              ;20
uuCallBack:
uuMod3 EQU $-uuDriverCode
       ld     a,0                                       ;22
       out    (6),a                                     ;24
       call   label                                     ;27
uuCall1 EQU $-uuDriverCode
uuMod1 EQU $-uuDriverCode
       ld     a,0                                       ;29
       out    (6),a                                     ;31
       ret                                              ;32
uuDriverSize EQU $ - uuDriverCode
uuDone:


 .endm

offCallBack EQU 20
